package cn.jly.boot.mp.custom_actuator.health;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;

/**
 * 自定义健康检查组件，必须以HealthIndicator结尾
 * 访问：http://localhost:8080/actuator/health
 *
 * @PackageName cn.jly.boot.mp
 * @ClassName MyComponentHealthIndicator
 * @Description 自定义健康检查组件
 * @Author 姬岚洋
 * @Date 2021/1/24 下午 4:25
 */
@Component
public class MyComponentHealthIndicator extends AbstractHealthIndicator {
    @Value("${my-server.ip}")
    private String myServerId;

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        if (StringUtils.isNotEmpty(myServerId)) {
            // 如果myServerIp存在，则应用健康

            builder.status(Status.UP);
            builder.withDetail("name", "lanyangji")
                    .withDetail("myServerIp", myServerId);
        } else {
            builder.status(Status.DOWN)
                    .withDetail("msg", "unknown myServerId");
        }
    }
}
